<?php

/**+----------------------------------------------------------------------
 * 作	   者: xinglou <xinglou1@foxmail.com>
 +----------------------------------------------------------------------
 * 创建日期：2015-06-24
 ------------------------------------------------------------------------
 * 文件描述：数据字典管理
 ------------------------------------------------------------------------
 * 升级记录：	
 ------------------------------------------------------------------------
 */
class FieldDictAction extends CommonAction
{

    /**
     * 显示数据字典管理页面
     */
    public function index()
    {
        $this->getTable($this->_request('table_name'), $this->_request('field_name')); // 获取当前数据库的所有数据表
        $this->display();
    }

    /**
     * 获取数据库中的所有数据表
     */
    public function getTable($table_name, $field_name)
    {
        $model = new FieldDictModel();
        $table_list = $model->query("select TABLE_NAME name
									   from information_schema.TABLES
									  where TABLE_SCHEMA = '" . C('DB_NAME') . "'
									  order by name");
        $this->assign("table_list", $table_list);
        
        // 查询当前数据表的所有字段（列）
        if (! empty($table_name)) {
            $field_list = $model->query("select COLUMN_NAME name
										   from information_schema.COLUMNS
										  where TABLE_SCHEMA = '" . C('DB_NAME') . "'
											and TABLE_NAME = '" . $table_name . "'");
            $this->assign("field_list", $field_list);
        }
        // 如果表名不为空，字段不为空，查出所有数据并分页显示
        if (! empty($table_name) && ! empty($field_name)) {
            import("ORG.Util.Page"); // 导入分页类
                                     // 取得满足条件的记录数
            $count = $model->where("table_name = '" . $table_name . "'
								and field_name = '" . $field_name . "'")->count();
            $p = $this->_page($model, 'seq', 'asc'); // 分页所需信息
            $page = new Page($count, $p['listRows']); // 创建分页对象
            $data = $model->field("table_name,
			 						field_name,
			 						field_value,
			 						field_mean,
			 						get_field_dict_name('field_dict', 'type', type) type_name,
			 						seq,
			 						remark,
			 						operator,
			 						get_user_name(operator) operator,
			 						get_formate_date(update_date),
			 						update_date")
                ->where("table_name = '" . $table_name . "'
						  	   and field_name='" . $field_name . "'")
                ->order("`" . $p['order'] . "` " . $p['sort'])
                ->limit($page->firstRow . ', ' . $page->listRows)
                ->select();
            $this->assign("list", $data);
            $show = $page->show();
            $this->assign("show", $show);
        }
    }

    /**
     * 获取数据表中的的字段（异步请求时调用）
     */
    public function getColumn()
    {
        $table_name = $this->_request('table_name');
        if (! empty($table_name)) {
            $model = new FieldDictModel();
            $data = $model->query("select COLUMN_NAME name
									 from information_schema.COLUMNS
									where TABLE_SCHEMA = '" . C('DB_NAME') . "'
									  and TABLE_NAME = '" . $table_name . "'");
            if (! empty($data)) {
                $this->ajaxReturn($data, "查询成功", 1);
            } else {
                $this->ajaxReturn(null, "没有找到对应的字段", 0);
            }
        } else {
            return false;
        }
    }

    /**
     * 这个方法显示添加页面
     */
    public function add()
    {
        $table_name = $this->_request('table_name');
        $field_name = $this->_request('field_name');
        $this->assign('type_list', set_field_dict_list('field_dict', 'type'));
        $this->assign('status_list', set_field_dict_list('field_dict', 'status'));
        $this->assign('table_name', $table_name);
        $this->assign('field_name', $field_name);
        $this->display();
    }

    /**
     * 将数据添加到数据库
     */
    public function insert()
    {
        $model = new FieldDictModel();
        $data = $model->create();
        if ($data !== false) {
            $data = $model->where("table_name = '" . $data['table_name'] . "'
							   and field_name = '" . $data['field_name'] . "'
							   and field_value = '" . $data['field_value'] . "'")->find();
            if (empty($data)) {
                if (false !== $model->add()) {
                    $this->success('数据字典添加成功', __APP__ . '?g=Admin&m=FieldDict&a=index&table_name=' . $this->_request('table_name') . '&field_name=' . $this->_request('field_name'));
                } else {
                    $this->error('数据字典添加失败');
                }
            } else {
                $this->error('该记录已经存在！');
            }
        } else {
            $this->error('数据验证错误！');
        }
    }

    /**
     * 这个方法显示修改页面
     */
    public function edit()
    {
        $table_name = $this->_get('table_name');
        $field_name = $this->_get('field_name');
        $field_value = $this->_get('field_value');
        $model = new FieldDictModel();
        $data = $model->field("table_name,
								field_name,
								field_value,
								field_mean,
								type,
								status,
								seq,
								remark")
            ->where("table_name = '" . $table_name . "' 
						   and field_name = '" . $field_name . "'
						   and field_value = '" . $field_value . "'")
            ->find();
        $this->assign('type_list', set_field_dict_list('field_dict', 'type'));
        $this->assign('status_list', set_field_dict_list('field_dict', 'status'));
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 这个方法将修改的数据保存到数据库
     */
    public function update()
    {
        $table_name = $this->_post('table_name');
        $field_name = $this->_post('field_name');
        $field_value = $this->_post('field_value');
        $model = new FieldDictModel();
        $data = $model->create();
        if (false !== $data) {
            if (false !== $model->where("table_name = '" . $table_name . "'
									and field_name = '" . $field_name . "'
									and field_value = '" . $field_value . "'")->save()) {
                $this->success('数据字典修改成功', __APP__ . '?g=Admin&m=FieldDict&a=index&table_name=' . $this->_request('table_name') . '&field_name=' . $this->_request('field_name'));
            } else {
                $this->error('数据字典添加失败');
            }
        } else {
            $this->error('数据验证错误');
        }
    }

    /**
     * 这个方法删除数据字典
     */
    public function delete()
    {
        $table_name = $this->_get('table_name');
        $field_name = $this->_get('field_name');
        $field_value = $this->_get('field_value');
        $model = new FieldDictModel();
        if (! empty($table_name) && ! empty($field_name) && ! empty($field_value)) {
            if (false !== $model->where("table_name = '" . $table_name . "' 
									and field_name = '" . $field_name . "' 
									and field_value = '" . $field_value . "'")->delete()) {
                redirect(__APP__ . '?g=Admin&m=FieldDict&a=index&table_name=' . $this->_request('table_name') . '&field_name=' . $this->_request('field_name'));
            } else {
                $this->error('数据字典删除失败！');
            }
        } else {
            $this->error('参数错误！');
        }
    }
}
?>